package com.hlg.xboot.utils;

import com.digi_zones.common.utils.DateUtils;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.json.JSONObject;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NetUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>Description:工具类</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 中视数讯</p>
 * <p>Site: www.digi-zones.com</p>
 *
 * @author gaol
 * @version 1.0
 * @date 2016/10/31
 */
public class Utils {


    /**
     * 时间戳转换日期字符串
     *
     * @param time
     * @return
     * @throws Exception
     */
    public static String timestampToDate(Long time) throws Exception {
        return DateUtils.format(new Date(time));
    }

    /**
     * 模糊查询
     * @param value
     * @return
     */
    public static String likeQuery(String value) {
        if (StringUtils.isEmpty(value)) {
            return value;
        }
        return "%" + value + "%";
    }

    /**
     * 前模糊查询
     * @param value
     * @return
     */
    public static String likeStartQuery(String value) {
        if (StringUtils.isEmpty(value)) {
            return value;
        }
        return value + "%";
    }

    /**
     * 后模糊查询
     * @param value
     * @return
     */
    public static String likeEndQuery(String value) {
        if (StringUtils.isEmpty(value)) {
            return value;
        }
        return "%" + value;
    }

    /**
     * IP地址转实际地址
     * @param serverUrl
     * @param ip
     * @return
     */
    public static String getAddressByIP(String serverUrl, String ip) {
        String loginAdress = "未分配或内网IP";
        if(!NetUtil.isInnerIP(ip)){
            String result = HttpUtil.get(serverUrl + ip);
            if(!StringUtils.isEmpty(result)){
                JSONObject jo = JSONUtil.parseObj(result);
                if(jo.containsKey("code") && jo.getInt("code") == 0){//获取成功
                    JSONObject data = jo.getJSONObject("data");

                    List<String> address = new ArrayList<String>();
                    String[] arrs = new String[]{"country", "area", "region", "city"};
                    for (String arr : arrs) {
                        if(data.containsKey(arr) && !StringUtils.isEmpty(data.getStr(arr))){
                            address.add(data.getStr(arr));
                        }
                    }
                    loginAdress = ArrayUtil.join(address.toArray(), "/");
                }
            }
        }
        return loginAdress;
    }

    /**
     * 按目前最大值生成下一位编码
     * @param parentCode
     * @param maxCode
     * @return
     */
    public static String generateCode(String parentCode, String maxCode) {
        String nextCode = ""+ (Integer.parseInt(maxCode.substring(parentCode.length())) + 1);
        return maxCode.substring(0, maxCode.length() - nextCode.length()) + nextCode;
    }

    /**
     * 四舍五入，保留6位小数
     * @param money
     * @return
     */
    public static String decimalFormat(Double money){
        String format = NumberUtil.decimalFormat(",###.000000", NumberUtil.round(money, 6));
        if(format.startsWith(".")){
            format = "0" + format;
        }
        format = subZeroAndDot(format);

        return "" + Double.parseDouble(format.replaceAll(",", ""));
    }

    /**
     * 去掉字符串后面多余的0
     * @param s
     * @return
     */
    public static String subZeroAndDot(String s){
        if(s.indexOf(".") > 0){
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;
    }

    public static <T> T nullToDefault(T t, T defaultValue){
        return t == null?defaultValue:t;
    }

    public static void main(String[] args){
        Double fee = 0d;
        System.out.println(fee.doubleValue());
    }

}
